package com.qigao.mall.service.impl;

import com.qigao.mall.api.TradeStaticService;
import com.qigao.mall.jdbc.mapper.TbTradeStaticMapper;
import com.qigao.mall.jdbc.model.TbTradeStatic;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName market-parent
 * @date 2020-07-2020-07-09 16:27
 * @Description:
 */
@Service
public class TradeStaticServiceImpl implements TradeStaticService {
    @Resource
    private TbTradeStaticMapper tbTradeStaticMapper;
    @Override
    public void addBatch(List<TbTradeStatic> tbTradeStatics) {
        for(TbTradeStatic tbTradeStatic:tbTradeStatics){
            TbTradeStatic oldTradeStatic=tbTradeStaticMapper.selectByUniqIndex(tbTradeStatic.getOrderDate(),tbTradeStatic.getUserNo());
            if(oldTradeStatic!=null){
                tbTradeStatic.setId(oldTradeStatic.getId());
                tbTradeStatic.setVersion(oldTradeStatic.getVersion());
                tbTradeStaticMapper.addStaticData(tbTradeStatic);
            }else {
                tbTradeStatic.setTotalSaleAmt(tbTradeStatic.getSaleAmt());
                tbTradeStatic.setTotalSaleNum(tbTradeStatic.getSaleNum());
                tbTradeStatic.setTotalBuyAmt(tbTradeStatic.getBuyAmt());
                tbTradeStatic.setTotalBuyNum(tbTradeStatic.getBuyNum());
                tbTradeStaticMapper.insertSelective(tbTradeStatic);
            }
        }
    }

    @Override
    public TbTradeStatic selectLastTrade(String userNo, Date orderDate) {
        return tbTradeStaticMapper.selectLastTrade(userNo,orderDate);
    }

    /**
     * 构建最后一次交易统计数据
     * @param orderDate
     */
    @Override
    public void initLasTradeStatic(TbTradeStatic lastTbTradeStatic,Date orderDate) {
        TbTradeStatic oldTradeStatic=tbTradeStaticMapper.selectByUniqIndex(orderDate,lastTbTradeStatic.getUserNo());
        if(oldTradeStatic==null){
            lastTbTradeStatic.setId(null);
            lastTbTradeStatic.setOrderDate(orderDate);
            lastTbTradeStatic.setCreateTime(new Date());
            tbTradeStaticMapper.insertSelective(lastTbTradeStatic);
        }
    }

    @Override
    public void updateBatch(List<TbTradeStatic> tbTradeStatics) {
        tbTradeStaticMapper.updateBatch(tbTradeStatics);
    }
}
